var app = getApp()
Page({
  data: {
    info: [],
    k: '',
    u: '',
    avatarUrl: '',
    is_show_time: 1,

    collect: [
      {
        typeId: 0,
        name: '商品收藏',
        url: '../favorites/favorites',
        imageurl: '../../image/receipt.png',
        nums: ''
      },
      {
        typeId: 0,
        name: '店铺收藏',
        url: '../favorites_store/favorites_store',
        imageurl: '../../image/payment.png',
        nums: ''
      },
      {
        typeId: 0,
        name: '浏览记录',
        url: '../views_list/views_list',
        imageurl: '../../image/payment.png',
        nums: ''
      }
    ],
    // orderItems
    orderItems: [

      {
        typeId: 0,
        name: '待付款',
        state: 'wait_pay',
        url: '../order_list/order_list?state=wait_pay',
        nums: '',
        cla: "icon-pay",
        calear: true,

      },
      {
        typeId: 1,
        name: '待发货',
        state: 'order_payed',
        url: '../order_list/order_list?state=order_payed',
        nums: '',
        cla: "icon-goods-deliver",
        calear: true,
      },
      {
        typeId: 2,
        name: '待收货',
        state: 'wait_confirm_goods',
        url: '../order_list/order_list?state=wait_confirm_goods',
        cla: "icon-goods-recipt",
        nums: '',
        calear: true,
      },
      {
        typeId: 3,
        name: '待评价',
        state: 'finish',
        url: '../order_list/order_list?state=finish',
        cla: "icon-evalute",
        nums: '',
        calear: true,
      },
      {
        typeId: 4,
        name: '退款/退货',
        state: '',
        url: '../member_refund/member_refund',
        cla: "icon-tuikuantuihuo",
        nums: '',
        calear: true,
      },
      {
        typeId: 5,
        name: '全部订单',
        state: '',
        url: '../order_list/order_list?state=all',
        nums: '',
        cla: "icon-orders",
        calear: true,

      }
    ],
  },


  onShow: function () {
    var that = this
    var arr = that.data.orderItems

    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    if (!key) {
      // wx.reLaunch({
      //   url: '../authorize_login/authorize_login',
      // })
    }

    that.setData({
      k: key,
      u: uid
    })

    var userinfo = wx.getStorageSync('userInfo')
    var avatarUrl = userinfo.avatarUrl

    if (key) {
      for (var i in arr) {
        arr[i].calear = true
      }
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_User&met=getUserInfo&typ=json',
        method: 'GET',
        data: { k: key, u: uid },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          if (res.data.status == 200) {

            that.setData({
              orderItems: arr,
              info: res.data.data,
              'orderItems[0].nums': res.data.data.order_count.wait,
              'orderItems[1].nums': res.data.data.order_count.payed,
              'orderItems[2].nums': res.data.data.order_count.confirm,
              'orderItems[3].nums': res.data.data.order_count.finish,
              'orderItems[4].nums': res.data.data.order_count.return,
              'collect[0].nums': res.data.data.favorites_goods_num,
              'collect[1].nums': res.data.data.favorites_shop_num,
              'collect[2].nums': res.data.data.footprint_goods_num,
              avatarUrl: avatarUrl,
              is_show_time: 0
            });
          }

        }
      })
    } else {
      that.setData({
        is_show_time: 0
      })
    }


  },
  toNav: function(e) {
    var key = wx.getStorageSync('key')
  	  if (key) {
  	    wx.navigateTo({
  	    	url: e.currentTarget.dataset.url
  	    })
  	  } else {
  		  wx.showModal({
  		    title: '登录提示',
  		    content: '此功能需要登录后才能使用',
  		    confirmText: '去登录',
  		    cancelText: '再逛逛',
  		    success: (res) => {
  		      if (res.confirm) {
  		        wx.navigateTo({
  		          url: `/pages/authorize_login/authorize_login`
  		        });
  		      } else {
  		        wx.switchTab({ url: '/pages/index/index' });
  		      }
  		    }
  		  });
  	  }
  },
  toTX: function(e) {
    var key = wx.getStorageSync('key')
  	  if (key) {
  	    wx.navigateTo({
  	      url: '/pages/web_view2/web_view2?url=' + encodeURIComponent('https://paycenter.ldtd168.com/index.php?ctl=Info&met=index'),
  	    });
  	  } else {
  		  wx.showModal({
  		    title: '登录提示',
  		    content: '此功能需要登录后才能使用',
  		    confirmText: '去登录',
  		    cancelText: '再逛逛',
  		    success: (res) => {
  		      if (res.confirm) {
  		        wx.navigateTo({
  		          url: `/pages/authorize_login/authorize_login`
  		        });
  		      } else {
  		        wx.switchTab({ url: '/pages/index/index' });
  		      }
  		    }
  		  });
  	  }
  },
  toLogin: function () {
  	    wx.navigateTo({
  	      url: '/pages/authorize_login/authorize_login',
  	    })
  },
  toLogout: function () {
  	  // https://ucenter.ldtd168.com/index.php?ctl=Login&met=logout
  	  var that = this
  	  
  	  wx.request({
  	    url: app.configData.ucenter.api_url + "/index.php?ctl=Login&met=logout&typ=json",
  	    data: {},
  	    method: 'GET',
  	    success: function (res) {
  			// 清空所有存储
  			      wx.clearStorageSync()
  			      wx.showToast({
  			        title: '已退出',
  			        icon: 'success'
  			      })
  			      
  			      // 跳转到登录页
  			      setTimeout(() => {
  			        wx.reLaunch({ url: '/pages/member/member' })
  			      }, 1500)
  	    }
  	  })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    var that = this
    wx.showToast({
      title: '加载中',//提示信息
      icon: 'loading',//成功显示图标
      duration: 2000//时间
    })
    wx.stopPullDownRefresh()
    that.onShow()
  },


})